package com.starlink.model.dtomapper;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.starlink.model.domain.StarTravelDO;
import com.starlink.model.request.StarTravelRequest;
import com.starlink.model.vo.StarTravelVO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;

import java.util.Optional;

/**
 * @author zhanghong
 */
@Mapper(componentModel = "spring", imports = {JSON.class, Optional.class, Lists.class})
public interface StarTravelDtoMapper {

    StarTravelDtoMapper INSTANCE = Mappers.getMapper(StarTravelDtoMapper.class);

    /**
    * DO to VO
    * @param starTravelDO
    * @return
    */
    @Mappings({
            @Mapping(target = "detailImg", expression = "java(Optional.ofNullable(starTravelDO.getDetailImg()).map(item -> JSON.parseArray(item, String.class)).orElse(Lists.newArrayList()))"),
    })
    StarTravelVO do2VO(StarTravelDO starTravelDO);

    /**
     * request to DO
     * @param request
     * @return
     */
    @Mappings({
            @Mapping(target = "detailImg", expression = "java(Optional.ofNullable(request.getDetailImg()).map(JSON::toJSONString).orElse(null))"),
    })
    StarTravelDO request2DO(StarTravelRequest request);
}
